<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Cookie;
use think\facade\Route;
use think\facade\Config;
use think\facade\Request;
use think\facade\View;

// 直接显示模板文件
// 通用路由
Route::rule(
    '/[:module]/[:controller]/[:action]',
    function ($module = 'index', $controller = '', $action = '') {
        try {
            // 执行控制箱操作
            if (in_array($module, ['test', 'tools'])) {
                // 执行控制箱操作
                $controller = str_replace(' ', '', ucwords(str_replace('-', ' ', $controller)));  // 中划线 => 驼峰式:大小写
                $action     = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));  // 中划线 => 驼峰式:大小写
                $action     = lcfirst($action);
                $file       = "app\\controller\\$module\\$controller";
                $file       = lcfirst($file);
                if (!class_exists($file)) {
                    echo $file . '类不存在！！';
                    die;
                }
                if (!method_exists($file, $action)) {
                    echo $file . '\\' . $action . '方法不存在！！';
                    die;
                }

                $ctrl = new $file($this->app);
                return $ctrl->$action($this->app->request);
            } else {
                /*// 直接显示模板
                $tpl_path = 'frontend/bootstrap/';
                $_path    = $this->app->getRootPath() . '/' . Config::get('view.view_dir_name') . '/';
                $_suffix  = Config::get('view.view_suffix');
                $template = $tpl_path .  $module . ($controller ? '/' . $controller : '') . ($action ? '/' . $action : '');
                $file     = $_path . $template . '.' . $_suffix;
                if (!is_file($file)) {
                    $template = $tpl_path .$module . ($controller ? '-' . $controller : '') . ($action ? '-' . $action : '');
                    $file     = $_path . $template . '.' . $_suffix;
                }
                if (!is_file($file)) {
                    die('模板文件不存在' . $file);
                }


                return View::fetch($template);*/

                $file = "app\\controller\\Template";
                $ctrl = new $file($this->app);
                return $ctrl->show($this->app->request);
            }
        } catch (\Exception $e) {
            throw new \think\Exception($e->getMessage(), $e->getCode(), $e);
        }
    }
);
